package zcw.com.lib_leet_code;

import java.util.Deque;
import java.util.LinkedList;

/**
 * Created by 朱城委 on 2022/2/11.<br><br>
 *
 * 简单：用栈实现队列
 */
public class Topic232 {
}

class MyQueue {

    private final Deque<Integer> stack1;
    private final Deque<Integer> stack2;

    private int front;

    public MyQueue() {
        stack1 = new LinkedList<>();
        stack2 = new LinkedList<>();
    }

    public void push(int x) {
        if(stack2.isEmpty()) {
            front = x;
        }
        stack2.push(x);
    }

    public int pop() {
        if(stack1.isEmpty()) {
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop());
            }
        }

        return stack1.pop();
    }

    public int peek() {
        if(!stack1.isEmpty()) {
            return stack1.peek();
        }

        return front;
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}

class MyQueue2 {

    private final Deque<Integer> stack1;
    private final Deque<Integer> stack2;

    private int front;

    public MyQueue2() {
        stack1 = new LinkedList<>();
        stack2 = new LinkedList<>();
    }

    public void push(int x) {
        if(stack1.isEmpty()) {
            front = x;
        }

        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }

        stack2.push(x);
        while (!stack2.isEmpty()) {
            stack1.push(stack2.pop());
        }
    }

    public int pop() {
        int result = stack1.pop();
        if(!stack1.isEmpty()) {
            front = stack1.peek();
        }
        return result;
    }

    public int peek() {
        return front;
    }

    public boolean empty() {
        return stack1.isEmpty();
    }
}
